// pages/user-login/user-login.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recommendUser: '', // 扫码结果
    codename: '获取验证码'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  getPhoneValue: function(e) {
    console.log("phone", e.detail.value)
    this.setData({
      phone: e.detail.value
    })
  },
  getverifyCode: function(e) {
    console.log("verifyCode", e.detail.value)
    this.setData({
      verifyCode: e.detail.value
    })
  },
  getCode: function() {
    var phone = this.data.phone;
    var that = this;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9]|19[0-8])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      var num = 4;
      var timer = setInterval(function() {
        num--;
        if (num <= 0) {
          clearInterval(timer);
          that.setData({
            codename: '重新发送',
            disabled: false
          })
        } else {
          that.setData({
            codename: num + "s"
          })
        }
      }, 1000)
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      var num = 4;
      var timer = setInterval(function() {
        num--;
        if (num <= 0) {
          clearInterval(timer);
          that.setData({
            codename: '重新发送',
            disabled: false
          })
        } else {
          that.setData({
            codename: num + "s"
          })
        }
      }, 1000)
      return false;
    } else {
      wx.request({
        url: app.globalData.baseUrl + 'system/mobile/sendCode',
        data: {
          'mobile': phone,
          'isDebug': 2
        },
        header: {
          'sign': app.globalData.sign,
          'token': app.globalData.token,
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        },
        method: 'POST',
        success(res) {
          console.log("send", res.data)
          if (res.code == 200) {
            wx.showToast({
              title: res.data.message,
              icon: 'success',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          }
          var num = 61;
          var timer = setInterval(function() {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              that.setData({
                codename: '重新发送',
                disabled: false
              })
            } else {
              that.setData({
                codename: num + "s"
              })
            }
          }, 1000)
        }
      })
    }
  },
  //获取验证码
  getVerificationCode() {
    this.getCode();
    var that = this
    that.setData({
      disabled: true
    })
  },
  // 扫码登入
  scanLogin() {
    const that = this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        that.setData({
          recommendUser: res.result
        })

      }
    })
  },
  logined: function(e) {
    console.log("logined", e);
    console.log("loginedtoken", app.globalData.token);
    var that = this;
    var phone = that.data.phone;
    var verifyCode = that.data.verifyCode;
    // var roleType = wx.getStorageSync('roleType');
    var userInfo = wx.getStorageSync('userInfo');
    var openid = wx.getStorageSync('openid');
    // var openid = '555555EMwzMt9jz66ku2ntjT3fqI'
    console.log('openid:', openid);
    var loginjudge = wx.getStorageSync('loginjudge');
    console.log('loginjudge:', loginjudge);
    console.log("phone", phone);
    console.log("verifyCode", verifyCode);
    console.log("roleType", 2);
    console.log("userInfo", userInfo);
    console.log("openId", openid);

    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9]|19[0-8])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      wx.request({
        url: app.globalData.baseUrl + 'doctor/member/reg',
        data: {
          'mobile': phone,
          'verifyCode': verifyCode,
          'roleType': 2,
          'openId': openid,
          'avatarUrl': userInfo.avatarUrl,
          'nickName': userInfo.nickName,
          'gender': userInfo.gender,
          'city': userInfo.city,
          'recommendUser': that.data.recommendUser
        },
        header: {
          'sign': app.globalData.sign,
          'token': app.globalData.token,
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        },
        method: 'POST',
        success(res) {
          console.log('医生登陆', res);

          if (res.statusCode == 200) {
            if (res.data.status_code == 200) {
              var toast = res.data.data[0].msg;
              wx.showToast({
                title: toast,
              });
              wx.setStorageSync("token", res.data.data[0].token);
              wx.setStorageSync("userid", res.data.data[0].userId);
              wx.setStorageSync("auditState", res.data.data[0].auditState);
              wx.setStorageSync("isLogin", 1);
              console.log('loginjudge', loginjudge);
              console.log('登陆后的token', res.data.data[0].token);
              app.globalData.token = res.data.data[0].token;
              wx.setStorageSync("roleType", 2);
              // if (!loginjudge && res.data.data[0].auditState != 2) {
              if (res.data.data[0].perfect == 1 || res.data.data[0].auditState == 3) {
                wx.redirectTo({
                  url: "../doctor-authentication-1/doctor-authentication-1"
                })
              } else {
                if (res.data.data[0].auditState == 1) {
                  wx.setStorageSync("auditState", 1);
                  wx.navigateTo({
                    url: '../doctor-authentication-3/doctor-authentication-3',
                  })
                  // wx.redirectTo({
                  //   url: '/pages/index/index',
                  // })
                } else if (res.data.data[0].auditState == 2) {
                  wx.setStorageSync("auditState", 2);
                  wx.reLaunch({
                    url: '../doctor-index/doctor-index',
                  })
                } else if (res.data.data[0].auditState == 3) {
                  wx.setStorageSync("auditState", 3);
                  return false;
                }
              }
              wx.request({
                url: app.globalData.baseUrl + 'doctor/memberTaskApi/addBeauty',
                data: {
                  'type': 1,
                  'userId': res.data.data[0].userId,
                },
                header: {
                  'sign': app.globalData.sign,
                  'token': app.globalData.token,
                  'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
                },
                method: 'POST',
                success(res) {
                  console.log('医生登陆美丽币', res);
                }
              })
            } else {
              wx.showToast({
                title: res.data.message,
                icon: 'none',
              })
            }
          }
        }
      })
    }
  }
})